package com.lpx.demo.demo.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.lpx.demo.demo.util.FileUtils;
import com.lpx.demo.demo.util.QRCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.UUID;

/**
 * @Description :后台控制层
 * @Date 2021/1/25 9:33
 * @Author lpx
 */
@Controller
@RequestMapping(value = "/home")
public class BaseHomeController {

    /**
     * @Description :工作台
     * @Date 2021/2/5 14:35
     * @Author lpx
     */
    @RequestMapping(value = "/workpalte")
    public String workpalte() {
        return "/home/workpalte";
    }

    /**
     * @Description :登录页面
     * @Date 2021/2/5 14:35
     * @Author lpx
     */
    @RequestMapping(value = "/login")
    public String loginIndex() {
        return "/home/login";

    }

    /**
     * @Description :注册页面
     * @Date 2021/2/5 14:35
     * @Author lpx
     */
    @RequestMapping(value = "/register")
    public String register() {
        return "/home/register";
    }

    /**
     * @Description :获取登录二维码
     * @Date 2021/2/25 13:50
         * @Author lpx
     */
    @RequestMapping(value = "/getQRcodebylogin")
    @ResponseBody
    public void getLoginQRcode(HttpServletResponse response) {
        //生成唯一二维码进行扫码登录操作
        // 系统临时目录
        String oqCodeName = UUID.randomUUID().toString().replaceAll("-", "");
        String tempPath = System.getProperty("java.io.tmpdir") + File.separator + oqCodeName + ".jpg";
        ServletOutputStream out = null;
        FileInputStream in = null;
        response.setContentType("multipart/form-data");
        try {
            QRCodeUtil.encode(oqCodeName, FileUtils.getFilePath("static/image/liaoliao.jpg"), new FileOutputStream(tempPath), true);
            in = new FileInputStream(tempPath);
            out = response.getOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
